package rpg.client.player.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import rpg.client.Instruction;
import rpg.client.model.ActorInfo;
import rpg.client.model.GameObject;

public class CharacterJDialog extends JDialog implements ActionListener {
    /**
	 * 
	 */
    private static final long serialVersionUID = 7420730266335223404L;

    public static void launch(GameJPanel gjp, ActorInfo ai) {
        StartCharacter sc = new StartCharacter(gjp, ai);
        SwingUtilities.invokeLater(sc);
    }

    private final MyPanel myPanel;
    private final GameJPanel game;
    private final String playerID;

    private final List<String> itemsToWear = new ArrayList<String>();

    public CharacterJDialog(GameJPanel gjp, ActorInfo ai) {
        super((java.awt.Frame) null, "Character", true);
        game = gjp;
        this.playerID = gjp.getPlayerID();

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setPreferredSize(new Dimension(300, 300));
        myPanel = new MyPanel(this, ai.getWearing(), ai.getInventory());
        add(myPanel, BorderLayout.SOUTH);

        AvatarPanel avatarPanel = new AvatarPanel(ai.getActor());
        add(avatarPanel, BorderLayout.NORTH);

        pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        debug(e);
        if (e.getSource() instanceof InventoryButton) {
            InventoryButton ib = (InventoryButton) e.getSource();
            if (!itemsToWear.contains(ib.getGameObjectID())) {
                itemsToWear.add(ib.getGameObjectID());
            }
            return;
        }
        else if (e.getSource() instanceof WearingButton) {
            WearingButton ib = (WearingButton) e.getSource();
            return;
        }
        else if (e.getSource() == myPanel.done) {
        }
        else if (e.getSource() == myPanel.cancel) {
            itemsToWear.clear();
        }

        setVisible(false);
        for (Instruction i : getInstructions()) {
            game.request(i);
        }
        dispose();
    }

    private void debug(Object o) {
        System.out.println("CharacterJPanel: " + o);
    }

    private List<Instruction> getInstructions() {
        List<Instruction> instructions = new ArrayList<Instruction>();
        for (String gameObjectID : itemsToWear) {
            instructions.add(Instruction.wear(playerID, gameObjectID));
        }
        return instructions;
    }
}

class InventoryButton extends JButton implements ActionListener {
    /**
	 * 
	 */
    private static final long serialVersionUID = -4624968452588948911L;

    private final String gameObjectID;
    private final String objectName;

    public InventoryButton(ActionListener listener, GameObject go) {
        super(go.getName());
        objectName = go.getName();
        gameObjectID = go.getId();
        addActionListener(this);
        addActionListener(listener);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        setText("I will put on " + objectName);
    }

    final String getGameObjectID() {
        return gameObjectID;
    }
}

class MyPanel extends JPanel {
    final JButton done = new JButton("Done");
    final JButton cancel = new JButton("Cancel");

    public MyPanel(ActionListener listener, List<GameObject> wearing, List<GameObject> inventory) {
        super(new GridLayout(4 + wearing.size() + inventory.size(), 1));

        add(new JLabel("Wearing:"));

        for (GameObject go : wearing) {
            System.out.println(go);
            WearingButton ib = new WearingButton(listener, go);
            add(ib);
        }

        add(new JLabel("Inventory:"));

        for (GameObject go : inventory) {
            System.out.println(go);
            if (go.isWearable()) {
                InventoryButton ib = new InventoryButton(listener, go);
                add(ib);
            }
            else {
                add(new JLabel(go.getName()));
            }
        }

        add(done);
        add(cancel);

        done.addActionListener(listener);
        cancel.addActionListener(listener);
    }
}

class StartCharacter implements Runnable {
    private final GameJPanel game;
    private final ActorInfo info;

    StartCharacter(GameJPanel gjp, ActorInfo ai) {
        super();
        game = gjp;
        info = ai;
    }

    public void run() {
        CharacterJDialog cjd = new CharacterJDialog(game, info);
        cjd.setVisible(true);
    }
}

class WearingButton extends JButton {
    public WearingButton(ActionListener listener, GameObject go) {
        super(go.getName());
        this.addActionListener(listener);
    }
}
